package com.chuji.crmcj.adapter;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.chuji.crmcj.R;
import com.chuji.crmcj.bean.InsUseHisInfo;
import com.chuji.crmcj.utils.UIUtils;

/**
 * Created by w.pitt on 2016/3/12.
 */
public class InsureHisUserAda extends BaseAdapter {

    private Context mContext;
    private InsUseHisInfo insUserHisInfo;
    private ListView mLvInsureHis;
    private LinearLayout mll_check_detail;
    boolean flag = false;
    private RelativeLayout mRl_insure;
    private LinearLayout ll;

    public InsureHisUserAda(Context mContext, InsUseHisInfo insUseHisInfo, ListView mLvInsureHis) {
        this.mContext = mContext;
        this.insUserHisInfo = insUserHisInfo;
        this.mLvInsureHis = mLvInsureHis;
    }

    @Override
    public int getCount() {
        return 5;
//        return insUserHisInfo.getApiParamObj().size() == 0 ? 0 : insUserHisInfo.getApiParamObj().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    private boolean isFirst;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        if (convertView == null) {
        convertView = View.inflate(UIUtils.getContext(), R.layout.layout_insure_his, null);
        mll_check_detail = (LinearLayout) convertView.findViewById(R.id.ll_check_detail);
        mRl_insure = (RelativeLayout) convertView.findViewById(R.id.rl_insure);
        ll = (LinearLayout) convertView.findViewById(R.id.Ll);

//        if (!isFirst) {
//            ll.setMinimumHeight(500);
//            isFirst = !isFirst;
//        } else {
//            ll.setMinimumHeight(0);
//            isFirst = !isFirst;
//        }
        mll_check_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAnimation();
            }
        });
        return convertView;
    }

    private void startAnimation() {
        int startHeight;
        final int targetHeight;
        if (!flag) {    //  展开的动画
            startHeight = 0;
            targetHeight = getMeasureHeight();
            flag = true;
            //safe_content.setVisibility(View.VISIBLE);
            //safe_content.getMeasuredHeight();  //  0
        } else {    //回收的动画
            flag = false;
            //safe_content.setVisibility(View.GONE);
            startHeight = getMeasureHeight();
            targetHeight = 0;
        }
        ll.setMinimumHeight(targetHeight);
        // 值动画
        ValueAnimator animator = ValueAnimator.ofInt(startHeight, targetHeight);
        //safe_content的布局属性  前缀看父窗体
        final LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mRl_insure.getLayoutParams();
        // 监听值的变化
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
                int value = (Integer) animator.getAnimatedValue(); // 运行当前时间点的一个值
                layoutParams.height = value;
                mRl_insure.setLayoutParams(layoutParams);// 刷新界面
                System.out.println(value);
            }
        });
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (flag) {
                    UIUtils.showToastSafe("箭头向上");

//                    safe_arrow.setImageResource(R.drawable.arrow_up);
                } else {
                    UIUtils.showToastSafe("箭头向下");
//                    safe_arrow.setImageResource(R.drawable.arrow_down);

                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        animator.setDuration(500);
        animator.start();
    }

    //onMeasure()  制定测量的规则
    // measure() 实际测量

    /**
     * 获取控件实际的高度
     * 测量控件的高
     */
    private int getMeasureHeight() {
        int width = mRl_insure.getMeasuredWidth();  //  由于宽度不会发生变化  宽度的值取出来
        mRl_insure.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;//  让高度包裹内容
        //    参数1  测量控件mode    参数2  大小
        int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);  //
        // mode+size
        int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.AT_MOST);// 我的高度 最大是1000
        // 测量规则 宽度是一个精确的值width, 高度最大是1000,以实际为准
        mRl_insure.measure(widthMeasureSpec, heightMeasureSpec); // 通过该方法重新测量控件
        return mRl_insure.getMeasuredHeight();
    }
}
